package com.hrsoft.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hrsoft.common.utils.PageUtils;
import com.hrsoft.common.utils.R;
import com.hrsoft.common.utils.ShiroUtils;
import com.hrsoft.modules.sys.entity.SysItemdetailsEntity;
import com.hrsoft.modules.sys.entity.SysItemsEntity;
import com.hrsoft.modules.sys.service.SysItemdetailsService;
import com.hrsoft.modules.sys.service.SysItemsService;
import com.hrsoft.common.exception.RRException;
import com.hrsoft.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 
 *
 * @author garfield.yue
 * @email yuedefeng101@163.com
 * @date 2020-03-19 20:51:34
 */
@RestController
@RequestMapping("sys/sysitems")
public class SysItemsController {
    @Autowired
    private SysItemsService sysItemsService;
    @Autowired
    private SysItemdetailsService sysItemDetailsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysitems:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysItemsService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:sysitems:info")
    public R info(@PathVariable("id") Long id){
		SysItemsEntity sysItems = sysItemsService.getById(id);

        return R.ok().put("sysItems", sysItems);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysitems:save")
    public R save(@RequestBody SysItemsEntity sysItems){
        SysUserEntity user = ShiroUtils.getUserEntity();
        sysItems.setCreatedOn(new Date());
        sysItems.setCreatedBy(user.getFullName());
        sysItems.setCreateUserId(user.getUserId().intValue());
		sysItemsService.save(sysItems);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysitems:update")
    public R update(@RequestBody SysItemsEntity sysItems){
        SysUserEntity user = ShiroUtils.getUserEntity();
        sysItems.setModifiedOn(new Date());
        sysItems.setModifiedBy(user.getFullName());
        sysItems.setModifiedUserId(user.getUserId().intValue());
		sysItemsService.updateById(sysItems);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysitems:delete")
    public R delete(@RequestBody Long[] ids){
        for(Long id : ids) {
            QueryWrapper<SysItemdetailsEntity> detailWrapper = new QueryWrapper<>();
            detailWrapper.eq("category_id", id);
            Integer count = sysItemDetailsService.count(detailWrapper);
            if (count > 0) {
                throw new RRException("id=[" + id + "], 存在明细项目，请先删除明细项目!");
            }
        }
		sysItemsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
